package com.wyzz.hr.util;

import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExpiresHeaderFilter implements Filter {

	public ExpiresHeaderFilter() {
		time = 0L;
		patterns = new String[0];
	}

	public void init(FilterConfig config) throws ServletException {
		String patternStr = config.getInitParameter("ContentPatterns");
		String values[] = patternStr.split("\\|");
		List list = new ArrayList();
		String arr$[] = values;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			String value = arr$[i$];
			if (value != null && !"".equals(value))
				list.add(value);
		}

		patterns = (String[]) list.toArray(new String[list.size()]);
		String timeStr = config.getInitParameter("max-age");
		try {
			time = Long.parseLong(timeStr);
		} catch (NumberFormatException e) {
			time = 0L;
		}
	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		if ((resp instanceof HttpServletResponse)
				&& (req instanceof HttpServletRequest)) {
			HttpServletRequest request = (HttpServletRequest) req;
			String url = request.getRequestURI();
			String arr$[] = patterns;
			int len$ = arr$.length;
			int i$ = 0;
			do {
				if (i$ >= len$)
					break;
				String pattern = arr$[i$];
				if (url.endsWith(pattern)) {
					HttpServletResponse response = (HttpServletResponse) resp;
					response.addHeader("Cache-Control", (new StringBuilder())
							.append("max-age=").append(time).append(
									", must-revalidate").toString());
					response.addDateHeader("Expires", Calendar.getInstance()
							.getTimeInMillis()
							+ time * 1000L);
					break;
				}
				i$++;
			} while (true);
		}
		chain.doFilter(req, resp);
	}

	public void destroy() {
	}

	private long time;
	private String patterns[];
	private static final String KEY_CONTENT_PATTERNS = "ContentPatterns";
	private static final String KEY_MAX_AGE = "max-age";
	private static final String SEPERATOR = "\\|";
}
